﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Namespace RemoteableObject

  ''' <summary>
  ''' This Object is used for the Remoting
  ''' </summary>
  ''' <remarks></remarks>
  <System.Runtime.InteropServices.ComVisible(False)>
  Public Class MyRemotableObject
    Inherits MarshalByRefObject

    Public Sub New()
      MyBase.New()
    End Sub

    'Die Einstellungen der LeaseTime sind die Default-Einstellungen

    ''' <summary>
    ''' Die Zeit, die ein Singleton Objekt grundsätzlich lebt. (Minuten)
    ''' </summary>
    ''' <remarks></remarks>
    Const TimeToLive As Integer = 5

    ''' <summary>
    ''' Wann soll abgefragt werden, ob die LeaseTime überschritten wurde. (Sekunden)
    ''' </summary>
    ''' <remarks></remarks>
    Const PollEvery As Integer = 10

    ''' <summary>
    ''' Um wie viel soll die Zeit, bei einem aufruf durch einen Client, verlängert werden (Minuten)
    ''' </summary>
    ''' <remarks></remarks>
    Const AddForRemoteCall = 2

    ''' <summary>
    ''' Hiermit kann die Lebensdauer eines Singleton-Objektes bestimmt werden.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function InitializeLifetimeService() As Object
      Dim lease As Lifetime.ILease = DirectCast(MyBase.InitializeLifetimeService, Lifetime.ILease)
      If lease.CurrentState = Lifetime.LeaseState.Initial Then
        lease.InitialLeaseTime = TimeSpan.FromMinutes(5)
        lease.SponsorshipTimeout = TimeSpan.FromSeconds(10)
        lease.RenewOnCallTime = TimeSpan.FromMinutes(2)
      End If
      Return lease
    End Function

    Public Function Authenticate(ByVal User As String, ByVal Pass As String) As String
      Return Cache.GetInstance().Authenticate(User, Pass)
    End Function


    Public Function SetValue(ByVal Token As String, ByVal Name As String, ByVal Value As String) As Boolean
      Cache.GetInstance().Value(Token, Name) = Value
      Return Cache.GetInstance().Result
    End Function

    Public Function GetValue(ByVal Name As String) As String
      Return Cache.GetInstance().Value("", Name)
    End Function

    Public Function DelValue(ByVal Token As String, ByVal Name As String) As Boolean
      Return Cache.GetInstance.DeleteValue(Token, Name)
    End Function

    Public Function GetDB(ByVal Name As String, Optional ByVal CreateIfNotExist As Boolean = True) As String
      Return Cache.GetInstance.GetDB(Name, CreateIfNotExist)
    End Function


    Public Function DeleteDB(ByVal Token As String, ByVal Name As String) As Boolean
      Return Cache.GetInstance.DeletDB(Token, Name)
    End Function

    Public Function DelPrivateValue(ByVal Token As String, Identifier As Guid, Name As String) As Boolean
      Return Cache.GetInstance.PrivateDeleteValue(Token, Name, Identifier)
    End Function

    Public Function GetPrivateValue(Identifier As Guid, Name As String) As String
      Return Cache.GetInstance.PrivateValue("", Name, Identifier)
    End Function

    Public Function SetPrivateValue(ByVal Token As String, Identifier As Guid, Name As String, Value As String) As Boolean
      Cache.GetInstance.PrivateValue(Token, Name, Identifier) = Value
      Return Cache.GetInstance.Result
    End Function

    Public Function ListDBs(Optional ByVal Identifier As String = "") As List(Of String)
      Return Cache.GetInstance.ListDBs(Identifier)
    End Function

    Public Function ListPrivateValues(Identifier As Guid, Optional Name As String = "") As Dictionary(Of String, String)
      Return Cache.GetInstance.ListPrivateValues(Identifier, Name)
    End Function

    Public Function ListValues(Optional ByVal Name As String = "") As Dictionary(Of String, String)
      Return Cache.GetInstance.ListValues(Name)
    End Function

    Public Function Revoke(ByVal Token As String) As Boolean
      Return Cache.GetInstance.Revoke(Token)
    End Function
  End Class
End Namespace